package com.sskj.community.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.android.arouter.utils.TextUtils;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.community.R;
import com.sskj.community.R2;
import com.sskj.community.presenter.PublishActivityPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.COMMUNITY_ACTIVITY_PUBLISH)
public class PublishActivity extends BaseActivity<PublishActivityPresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tvRight)
    TextView tvRight;
    @BindView(R2.id.et_title)
    EditText etTitle;
    @BindView(R2.id.et_content)
    EditText etContent;
    @BindView(R2.id.tv_title_num)
    TextView tvTitleNum;
    @BindView(R2.id.tv_content_num)
    TextView tvContentNum;

    private String inputTitle;
    private String inputContent;

    @Override
    public int getLayoutId() {
        return R.layout.community_activity_publish;
    }

    @Override
    public PublishActivityPresenter getPresenter() {
        return new PublishActivityPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        tvTitle.setText(App.INSTANCE.getString(R.string.community_publishActivity1));
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText(App.INSTANCE.getString(R.string.community_publishActivity2));
        setTitleEditListener();
        setContentEditListener();
    }

    private void setContentEditListener() {
        etContent.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable editable) {
                inputContent = etContent.getText().toString();
                tvContentNum.setText(inputContent.length() + "/2000");
            }
        });
    }

    @Override
    public void initData() {
        ClickUtil.click(300, tvRight, () -> {
            if (TextUtils.isEmpty(etTitle.getText().toString())||TextUtils.isEmpty(etContent.getText().toString())){
                ToastUtil.showShort(App.INSTANCE.getString(R.string.community_publishActivity3));
            }else {
                mPresenter.SubmitText(etTitle.getText().toString(),etContent.getText().toString());
            }
        });

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, PublishActivity.class);
        context.startActivity(intent);
    }

    private void setTitleEditListener() {
        etTitle.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable editable) {
                inputTitle = etTitle.getText().toString();
                tvTitleNum.setText(inputTitle.length() + "/50");
            }
        });
    }

    public void setTextSuccess() {
        ToastUtil.showShort(App.INSTANCE.getString(R.string.community_publishActivity4));
        finish();
    }
}
